//
//  LSPhotoBrowserCell.swift
//  MicroBlog
//
//  Created by lj on 15/9/14.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
import SDWebImage
import SVProgressHUD

class LSPhotoBrowserCell: UICollectionViewCell {
    
    var url: NSURL? {
        
        didSet {
            
            imageView.sd_setImageWithURL(url, placeholderImage: nil, options: [SDWebImageOptions.RetryFailed,SDWebImageOptions.RefreshCached]) { (image, error, _, _) -> Void in
                
                // 判断图像是否下载完成
                if error != nil {
                    SVProgressHUD.showInfoWithStatus("您的网络不给力")
                    return
                }
                self.setImagePosition()
            }
        }
    }
    
    private func setImagePosition() {
        let size = imageDisplaySize(imageView.image!)
        
        imageView.frame = CGRect(origin: CGPointZero, size: size)
        
        if size.height < scrollView.bounds.height {
            let y = (scrollView.bounds.height - size.height) * 0.5
            
            scrollView.contentInset = UIEdgeInsets(top: y, left: 0, bottom: 0, right: 0)
        }else {
            scrollView.contentSize = size
        }
        
    }
    
    private func imageDisplaySize(image: UIImage) ->CGSize {
        let scale = image.size.height/image.size.width
        
        let width = scrollView.bounds.width
        
        let height = scale * width
        
        return CGSize(width: width, height: height)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        contentView.addSubview(scrollView)
        scrollView.addSubview(imageView)
        
        contentView.addSubview(indicator)
        
        let rect = bounds
        //        rect.size.width += 20
        
        scrollView.frame = rect
        indicator.center = scrollView.center
        
        prepareForScrollView()
    }
    
    private func prepareForScrollView() {
        scrollView.delegate = self
        
        scrollView.minimumZoomScale = 0.5
        scrollView.maximumZoomScale = 2.0
    }
    
    private lazy var scrollView = UIScrollView()
    private lazy var imageView:UIImageView = UIImageView()
    private lazy var indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
    
}

extension LSPhotoBrowserCell: UIScrollViewDelegate {
    
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return imageView
    }
    
    func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
        
        // 调整 Y 值
        var offsetY = (scrollView.bounds.height - imageView.frame.height) * 0.5
        // 调整 X 值
        var offsetX = (scrollView.bounds.width - imageView.frame.width) * 0.5
        // 调整 offsetY 避免 < 0 之后，顶部内容看不到
        offsetY = (offsetY < 0) ? 0 : offsetY
        offsetX = (offsetX < 0) ? 0 : offsetX
        
        // 重新设置间距
        scrollView.contentInset = UIEdgeInsets(top: offsetY, left: offsetX, bottom: 0, right: 0)
    }
    
}
